Option Strict Off
Option Explicit On
Imports SlimDX
Public Class frmSelectDevice
    Inherits System.Windows.Forms.Form


    Dim m_callback As Object

    Public Sub SelectDevice(ByRef callback As Object)

        If callback Is Nothing Then Exit Sub
        m_callback = callback

        Dim dm As Direct3D9.DisplayMode

        If g_d3dpp.Windowed = 0 Then

            m_callback.InvalidateDeviceObjects()

            D3DUtil_ResetWindowed()

            m_callback.RestoreDeviceObjects()

        End If

        Me.ShowDialog()

        m_callback = Nothing

    End Sub


    Private Sub cboAdapter_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboAdapter.SelectedIndexChanged

        Dim devtype As Direct3D9.DeviceType

        If (cboDevice.SelectedIndex = 1) Then
            devtype = Direct3D9.DeviceType.Reference
        Else
            devtype = Direct3D9.DeviceType.Hardware
        End If

        Call UpdateModes((cboAdapter.SelectedIndex), devtype)

    End Sub

    Private Sub cboDevice_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboDevice.SelectedIndexChanged
        Dim devtype As Direct3D9.DeviceType

        If (cboDevice.SelectedIndex = 1) Then
            devtype = Direct3D9.DeviceType.Reference
        Else
            devtype = Direct3D9.DeviceType.Hardware
        End If

        Call UpdateModes((cboAdapter.SelectedIndex), devtype)

    End Sub


    Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click

        m_callback = Nothing
        Me.Close()

    End Sub

    Private Sub cmdOk_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOk.Click

        On Error Resume Next

        Dim bAdapterChanged As Boolean
        Dim bRasterizerChanged As Boolean
        Dim bRef As Boolean
        Dim lWindowed As Integer
        Dim AdapterID As Integer
        Dim ModeID As Integer
        Dim devtype As Direct3D9.DeviceType

        AdapterID = cboAdapter.SelectedIndex
        ModeID = cboFullScreenMode.SelectedIndex

        ' see if user changed adapters
        If g_lCurrentAdapter <> AdapterID Then bAdapterChanged = True

        bRef = g_Adapters(g_lCurrentAdapter).bReference

        If (cboDevice.SelectedIndex = 1) Then
            devtype = Direct3D9.DeviceType.Reference
        Else
            devtype = Direct3D9.DeviceType.Hardware
        End If

        ' see if user changed rasterizers
        If (devtype = Direct3D9.DeviceType.Reference And bRef = False) Then bRasterizerChanged = True
        If (devtype = Direct3D9.DeviceType.Hardware And bRef = True) Then bRasterizerChanged = True


        If optRenderingMode(1).Checked = True Then
            lWindowed = 0
        Else
            lWindowed = 1
        End If

        ' if they didnt change adapters or switch to refrast, then we can just use reset
        If bAdapterChanged = False And bRasterizerChanged = False Then

            'If trying to go Fullscreen
            If lWindowed = 0 Then

                'call g_dev.reset
                Call D3DUtil_ResizeFullscreen(g_focushwnd, (cboFullScreenMode.SelectedIndex), devtype)

            Else

                Call D3DUtil_ResizeWindowed(g_focushwnd)

            End If

            'tell user needs to restore device objects
            m_callback.RestoreDeviceObjects()

            'exit modal dialog
            Me.Close()
            Exit Sub

        End If

        Discard(g_dev)

        D3DUtil_ReleaseAllTexturesFromPool()

        'tell user to lose reference counts in its objects device objects
        m_callback.InvalidateDeviceObjects()
        m_callback.DeleteDeviceObjects()

        'Reinitialize D3D
        If lWindowed = 0 Then
            D3DUtil_InitFullscreen(PCanvas, g_focushwnd, AdapterID, ModeID, devtype, True)
        Else
            D3DUtil_InitWindowed(PCanvas, g_focushwnd, AdapterID, devtype, True)
        End If


        'tell user to re-create device objects
        m_callback.InitDeviceObjects()

        'tell user to restore device objects
        m_callback.RestoreDeviceObjects()

        'exit modal dialog
        Me.Close()

    End Sub

    Private Sub frmSelectDevice_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Call D3DEnum_BuildAdapterList()
        Call UpdateAdapters()
        Call UpdateDevices(g_lCurrentAdapter)
        Call UpdateModes(g_lCurrentAdapter, g_Adapters(g_lCurrentAdapter).DeviceType)
        cboAdapter_SelectedIndexChanged(cboAdapter, New System.EventArgs())
        cboDevice_SelectedIndexChanged(cboDevice, New System.EventArgs())
    End Sub

    Private Sub UpdateAdapters()

        Dim i, j As Integer
        Dim sDescription As String

        cboAdapter.Items.Clear()

        For i = 0 To g_lNumAdapters - 1

            sDescription = vbNullString
            Try


                sDescription = sDescription & g_Adapters(i).d3dai.Description & vbCrLf ' Chr(g_Adapters(i).d3dai.Description(j))

                sDescription = Replace(sDescription, Chr(0), " ")
                cboAdapter.Items.Add(sDescription)
            Catch ex As Exception

            End Try

        Next

        cboAdapter.SelectedIndex = g_lCurrentAdapter

    End Sub

    Private Sub UpdateDevices(ByRef adapter As Integer)

        Dim i As Integer

        cboDevice.Items.Clear()

        cboDevice.Items.Add("HAL")
        cboDevice.Items.Add("REF")

        'If g_Adapters(g_lCurrentAdapter).bReference Then
        If g_Adapters(adapter).bReference Then
            cboDevice.SelectedIndex = 1
        Else
            cboDevice.SelectedIndex = 0
        End If

    End Sub

    Private Sub UpdateModes(ByRef adapter As Integer, devtype As Direct3D9.DeviceType)

        Dim i As Integer
        Dim pAdapter As D3DUTIL_ADAPTERINFO
        Dim sModeString As String

        cboFullScreenMode.Items.Clear()
        D3DEnum_BuildDisplayModeList(adapter, devtype)

        With g_Adapters(adapter).DevTypeInfo(devtype)
            For i = 0 To .lNumModes - 1
                sModeString = .Modes(i).lWidth & " x "
                sModeString = sModeString & .Modes(i).lHeight & " x "
                If .Modes(i).formato = Direct3D9.Format.X8R8G8B8 Or _
                    .Modes(i).formato = Direct3D9.Format.A8R8G8B8 Or _
                    .Modes(i).formato = Direct3D9.Format.R8G8B8 Then
                    sModeString = sModeString & "32"
                Else
                    sModeString = sModeString & "16"
                End If

                cboFullScreenMode.Items.Add(sModeString)
            Next
            If cboFullScreenMode.Items.Count > 0 Then
                optRenderingMode(1).Enabled = True
                cboFullScreenMode.SelectedIndex = .lCurrentMode
            Else
                optRenderingMode(1).Enabled = False
                optRenderingMode(0).Checked = 1
            End If
        End With

    End Sub

    Private Sub optRenderingMode_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optRenderingMode.CheckedChanged
        If eventSender.Checked Then
            Dim Index As Short = optRenderingMode.GetIndex(eventSender)

            If Index = 1 Then
                cboFullScreenMode.Enabled = True
            Else
                cboFullScreenMode.Enabled = False
            End If

        End If
    End Sub
End Class